# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                        Dinis Marques                            #
#                         Snake Python                            #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import turtle
import random

# head orientation
h = [0]

# score
a = [0]
b = [0]

# food coord
fcoord = [0, 0, 0]

# position
pos = []

# global gamestart
# gamestart=False
global start_time
start_time =0
food=[]
at_food=0


def home(x, y):
    x = 0
    y = 0
    a[0] = 0
    b[0] = 0
    h[0] = 0
    fcoord[2] = 0
    pos[:] = []
    turtle.hideturtle()
    turtle.clear()
    turtle.pu()
    turtle.color("black")
    turtle.goto(0, 0)
    turtle.write("Play")
    turtle.title("Snake")
    turtle.onscreenclick(start)
    turtle.mainloop()


def level_1():
    turtle.clear()
    turtle.pu()
    turtle.speed(0)
    turtle.pensize(20)
    turtle.color("grey")
    turtle.goto(-220, 220)
    turtle.pd()
    turtle.goto(220, 220)
    turtle.goto(220, -220)
    turtle.goto(-220, -220)
    turtle.goto(-220, 220)
    turtle.pu()
    turtle.goto(0, 0)


def start(x, y):
    turtle.onscreenclick(None)

    level_1()

    tfood = turtle.Turtle()
    tfood.hideturtle()
    tfood.pu()
    tfood.speed(0)
    tfood.shape("square")
    tfood.color("red")

    tscore = turtle.Turtle()
    tscore.hideturtle()
    tscore.pu()
    tscore.speed(0)
    tscore.goto(100, -250)
    tscore.write("Score:" + str(a[0]), align="center", font=(10))

    while x > -210 and x < 210 and y > -210 and y < 210:
        if fcoord[2] == 0:
            food(tfood)
            fcoord[2] = 1
        turtle.onkey(u, "Up")
        turtle.onkey(l, "Left")
        turtle.onkey(r, "Right")
        turtle.onkey(d, "Down")
        turtle.listen()
        move()
        x = turtle.xcor()
        y = turtle.ycor()
        if x > fcoord[0] * 20 - 5 and x < fcoord[0] * 20 + 5 and y > fcoord[1] * 20 - 5 and y < fcoord[1] * 20 + 5:
            fcoord[2] = 0
            tfood.clear()
            a[0] += 1
            tscore.clear()
            tscore.write("Score:" + str(a[0]), align="center", font=(10))

        if len(pos) > 1:
            for i in range(1, len(pos)):
                if x < pos[i][0] + 5 and x > pos[i][0] - 5 and y < pos[i][1] + 5 and y > pos[i][1] - 5:
                    tscore.clear()
                    tfood.clear()
                    gameover()
    tscore.clear()
    tfood.clear()
    gameover()


# Food
def food(tfood):
    x = random.randrange(-8, 8, 1)
    y = random.randrange(-8, 8, 1)
    fcoord[0] = x
    fcoord[1] = y
    tfood.hideturtle()
    tfood.pu()
    tfood.shape("square")
    tfood.color("red")
    tfood.goto(x * 20, y * 20)
    tfood.stamp()


# Up
def u():
    if h[0] == 270:
        pass
    else:
        h[0] = 90


# Down
def d():
    if h[0] == 90:
        pass
    else:
        h[0] = 270


# Left
def l():
    if h[0] == 0:
        pass
    else:
        h[0] = 180


# Right
def r():
    if h[0] == 180:
        pass
    else:
        h[0] = 0


def move():
    turtle.pensize(1)
    turtle.color("black")
    turtle.pu()
    turtle.speed(3)
    turtle.setheading(h[0])
    turtle.shape("square")
    turtle.stamp()
    turtle.fd(20)
    x = turtle.xcor()
    y = turtle.ycor()
    if b[0] > a[0]:
        turtle.clearstamps(1)
        pos.insert(0, [round(x), round(y)])
        pos.pop(-1)
    else:
        pos.insert(0, [round(x), round(y)])
        b[0] += 1


def gameover():
    turtle.onscreenclick(None)
    turtle.speed(0)
    turtle.pu()
    turtle.goto(0, 150)
    turtle.color("red")
    turtle.write("Game Over", align="center", font=(10))
    turtle.goto(0, 50)
    turtle.write("Score:" + str(a[0]), align="center", font=(10))
    turtle.goto(200, -200)
    turtle.write("(Click anywhere to return to the main menu)", align="right", font=(0.0000001))
    turtle.onscreenclick(home)
    turtle.mainloop()


def screen():
    # Set up the screen
    win = turtle.Screen()
    win.title("Snake by hhy")
    win.bgcolor("white")
    # win.setup(width=600, height=600)
    win.setup(width=660, height=740)
    win.tracer(0)  # Turns off the screen updates for game speed optimization
    # win.onclick(clear_text_and_continue)  # 鼠标点击后开始
    # while not gamestart:
    #     win.onclick(clear_text_and_continue)  # 鼠标点击后开始

    border = turtle.Turtle()
    border.penup()  # 抬起画笔，以便在移动到起始绘制点时不留下痕迹
    border.color("black")  # 边界线的颜色
    border.goto(-250, -290)  # 边界线左下角的起始点
    border.pendown()  # 放下画笔准备绘制
    border.speed(0)  # 设置绘制边界的速度为最快
    border.pensize(3)
    # 绘制正方形边界
    for _ in range(4):
        border.forward(500)  # 向前移动 500 像素
        border.left(90)  # 向左转 90 度
    border.goto(-250, 210)  # 边界线左下角的起始点
    # 绘制正方形边界
    for _ in range(2):
        border.forward(500)  # 向前移动 500 像素
        border.left(90)  # 向左转 90 度
        border.forward(80)  # 向前移动 500 像素
        border.left(90)  # 向左转 90 度
    border.hideturtle()

    text_turtle = turtle.Turtle()
    text_turtle.hideturtle()  # 隐藏写文本的 turtle
    text_turtle.penup()  # 抬起画笔
    # text_turtle.color("black")  # 设置文本颜色
    text_turtle.goto(-25, 238)  # 移动到屏幕中心
    a = "Contact:    Time:     Motion:     "
    text_turtle.write(a, align="center", font=("Arial", 20, "bold"))  # 在屏幕中写文本

    text_start = turtle.Turtle()
    text_start.hideturtle()  # 隐藏写文本的 turtle
    text_start.penup()  # 抬起画笔
    # text_turtle.color("black")  # 设置文本颜色
    text_start.goto(-220, 50)  # 移动到屏幕中心
    text_start.write("Welcome to hhy's version of snake ....\n"
                     "\n"
                     "You are going to use the 4 arrow keys to move the snake\n"
                     "around the screen,trying to consume all the food items\n"
                     "before the monster catches you ....\n"
                     "\n"
                     "Click anywhere on the screen to start the game,have fun!!", align="left",
                     font=("Arial", 13, "normal"))  # 在屏幕中写文本

    # text_start.clear()  # 调试时注释

    # 初始化计时器显示的 turtle
    timer_turtle = turtle.Turtle()
    timer_turtle.hideturtle()  # 隐藏 timer_turtle
    timer_turtle.penup()  # 抬起画笔
    timer_turtle.goto(15, 238)  # 将 timer_turtle 移到屏幕上方
    timer_turtle.color("black")
    timer_turtle.write("0", align="center", font=("Arial", 20, "bold"))

    # motion移动的 turtle
    motion_turtle = turtle.Turtle()
    motion_turtle.hideturtle()  # 隐藏 timer_turtle
    motion_turtle.penup()  # 抬起画笔
    motion_turtle.goto(190, 238)  # 将 timer_turtle 移到屏幕上方
    motion_turtle.color("black")
    motion_turtle.write("Paused", align="center", font=("Arial", 20, "bold"))

    # contact分数的 turtle
    contact_turtle = turtle.Turtle()
    contact_turtle.hideturtle()  # 隐藏 timer_turtle
    contact_turtle.penup()  # 抬起画笔
    contact_turtle.goto(-100, 238)  # 将 timer_turtle 移到屏幕上方
    contact_turtle.color("black")
    contact_turtle.write("0", align="center", font=("Arial", 20, "bold"))

    # Snake head
    head = turtle.Turtle()
    head.speed(0)
    head.shape("square")
    head.color("red")
    head.penup()
    head.goto(0, 0)
    head.direction = "stop"

    # Monster
    monster = turtle.Turtle()
    monster.speed(0)
    monster.shape("square")
    monster.color("purple")
    monster.penup()
    monster.goto(0, -100)

    # Snake body
    segments = []

    # Score
    score = 0

    # Keyboard bindings
    win.listen()
    # win.onkeypress(go_up, "Up")
    # win.onkeypress(go_down, "Down")
    # win.onkeypress(go_left, "Left")
    # win.onkeypress(go_right, "Right")
    # win.onclick(clear_text_and_continue)  # 鼠标点击后开始
    win.mainloop()


# def clear_text_and_continue(x, y):  # 清除文本并开始计时
#     if start_time == 0:  # 在鼠标点击调用
#         text_start.clear()
#         start_timer()


# # # # # # # # # # # # # # # # # # # # # #
# Main                                    #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
    screen()


